import Cookies from 'js-cookie'

const state = {
    sidebarOpen: localStorage.getItem('sidebar-status') === 'true',
    locale: Cookies.get('locale') ? Cookies.get('locale') : 'zh-CN',
    device: 'desktop',
    version: process.env.APP_VERSION,
    editPwdDialog: false,
    windowSize: {width: 0, height: 0},
    hoverMenu: {},
    routeMatched: []
}

const mutations = {
    TOGGLE_SIDEBAR: (state) => {
        // console.log('TOGGLE_SIDEBAR', state)
        state.sidebarOpen = !state.sidebarOpen
        localStorage.setItem('sidebar-status', state.sidebarOpen + '')
    },

    TOGGLE_DEVICE: (state, device) => {
        state.device = device
    },
    SET_LOCALE: (state, locale) => {
        Cookies.set('locale', locale, {expires: 1000})
        state.locale = locale;
    },
    SET_WINDOW_SIZE: (state, data) => {
        state.windowSize = data;
    },
    SET_HOVER_MENU: (state, data) => {
        state.hoverMenu = data
    },
    SET_ROUTE_MATCHED: (state, data) => {
        state.routeMatched = data;
    }
}

const actions = {
    toggleSideBar({commit}) {
        commit('TOGGLE_SIDEBAR')
    },
    locale({commit}, locale) {
        commit('SET_LOCALE', locale);
    },
    setWindowSize({commit}, data) {
        commit('SET_WINDOW_SIZE', data)
    },

}
const getters = {
    getSize: (state) => state.windowSize,
    getHoverMenu: (state) => state.hoverMenu,
    getRouteMatched: (state) => state.routeMatched,
    submenus:(state,e,d)=>{
        const hoverMenu = e.getHoverMenu;
        if (hoverMenu && Object.keys(hoverMenu).length > 2) return hoverMenu;
        return e.getRouteMatched
    },
    hasSubmenu:(state,e)=>{
        const submenus =e.submenus
        return submenus&&submenus.children&&submenus.children.length>1
    }
}
export default {
    namespaced: false,
    state,
    mutations,
    actions,
    getters
}
